<template>
  <div>
    <van-nav-bar title="登录" left-arrow @click-left="onClickLeft"/>
    <van-form @submit="onSubmit">
      <van-field
        v-model="query.username"
        name="用户名"
        placeholder="请输入账号"
        :rules="[{ required: true, message: '请填写账号' }]"
      />
      <van-field
        v-model="query.password"
        type="password"
        name="密码"
        placeholder="请输入密码"
        :rules="[{ required: true, message: '请填写密码' }]"
      />
      <div style="margin: 16px;">
        <van-button block type="primary" native-type="submit">提交</van-button>
      </div>
    </van-form>
    <div class="am-flexbox-item">
      <a @click="$router.push('/registe')">还没有账号，去注册~</a>
    </div>
  </div>
</template>

<script>
import { request, setToken } from "../utils/index.js";
export default {
  data() {
    return {
      query: {
        username: "test",
        password: "test"
      }
    };
  },
  methods: {
    async onSubmit(values) {
      console.log("submit", values);
      const res = await request.post("/user/login", this.query);
      console.log(res);
      if (res.data.status === 200) {
        this.$toast.success("登录成功");
        setToken(res.data.body.token);
        this.$router.go(-1);
      } else {
        this.$toast.fail("账号或密码错误");
      }
    },
    onClickLeft() {
      this.$router.go(-1);
    }
  }
};
</script>
<style lang="less" scoped>
/deep/ .van-form {
  margin-top: 30px;
  .van-cell {
    padding: 0 10px;
    .van-field__body,
    .van-field__control {
      height: 60px;
      font-size: 17px;
      padding: 2px 0;
      border: 0;
      color: #000;
    }
  }
  .van-button {
    width: 345px;
    height: 50px;
  }
}
.am-flexbox-item {
  height: 70px;
  line-height: 70px;
  margin: 0 10px;
  text-align: center;
  border-bottom: 1px solid #cccccc;
  a {
    color: #666;
  }
}
</style>
